$(document).ready
(
	function()
	{
		if(!IsLoggedOn) showLogonScreen();
		else populateCurrentPage();

//		$(".diary-control-arrow").click(arrowClick);
		

//		alert("Is logged on = " + IsLoggedOn);
//		alert("Current page = " + CurrentPage);
	}
	
//load	
)

function showLogonScreen()
{
	$("#logon").slideDown(2000);
	$("#logon-button").click(tryLogon);
	$("#logon-username").keyup(function(event){if(event.which==13)tryLogon();});
	$("#logon-password").keyup(function(event){if(event.which==13)tryLogon();});
	$("#logon-button").hover(function(){$(this).addClass("logon-button-hover");},function(){$(this).removeClass("logon-button-hover");});
	$("#menu-admin").hide();
	$("#menu-logoff").hide();
}

function tryLogon()
{
//	alert ("try to logon");
	$("#logon-message").slideUp(1000);
//	$("#login-dummy").load('model/ajax/check-credentials.php','UserName='+$("#logon-username").val()+'&Password='+$("#logon-password").val()+'&SessionID='+SessionID+'&UserID='+UserID,tryLogonCallback)

	$.post
	('model/ajax/check-credentials.php',
		{
			UserName:$("#logon-username").val(),
			Password:$("#logon-password").val()

		},
		tryLogonCallback
	)	
}

function tryLogonCallback(data)
{
	var arr = data.split(",");
	IsLoggedOn=(arr[0]=="1")?1:0;
	UserID=arr[1];
	SessionID=arr[2];
	
//	alert ("IsLoggedOn="+IsLoggedOn);

	if(IsLoggedOn)
	{
		$("#logon").slideUp(2000,function(){$("#logon-message").html("");});
		populateCurrentPage();
	}
	else
	{
		$("#logon-message").html("Login Incorrect - try again");
		$("#logon-message").slideDown(800);
	}
}

function populateCurrentPage()
{
	$("#menu-home").click(function(){loadNewPage("home");});
	$("#menu-people").click(function(){loadNewPage("people");});
	$("#menu-musiclist").click(function(){loadNewPage("musiclist");});
	$("#menu-events").click(function(){loadNewPage("events");});
	$("#menu-attendance").click(function(){loadNewPage("attendance");});
	$("#menu-payroll").click(function(){loadNewPage("payroll");});
//	$("#menu-logoff").click(function(){IsLoggedOn=0;UserID=-1;SessionID=-1;loadNewPage("views/diary/diary.php");});
//	if(UserID==1)$("#menu-admin").show();else $("#menu-admin").hide();
//	$("#menu-logoff").show();
	$(".menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});


	switch (CurrentPage)
	{
		case 'home':
		default:
			loadHome();
			break;
			
		case 'people':
			loadPeople();
			break;
			
		case 'musiclist':
			loadMusicList();
			break;
			
		case 'events':
			loadEvents();
			break;
			
		case 'attendance':
			loadAttendance();
			break;
			
		case 'payroll':
			loadPayroll();
			break;			
	}
}

function loadNewPage(pageURL)
{
//	alert (pageURL);
	$("#new-page-form-page").val(pageURL); 
	$("#new-page-form-SessionID").val(SessionID); 
	$("#new-page-form-UserID").val(UserID); 
	$("#new-page-form").submit();
}